package com.example.sjly.ui.scheduling;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.sjly.R;

import java.util.LinkedList;
//listview更新相关数据

public class SchedulingMyAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<Data> mData;

    public SchedulingMyAdapter() {}

    public SchedulingMyAdapter(LinkedList<Data> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if(convertView == null){

            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_scheduling_messagegather_smalllayout,parent,false);
            holder = new ViewHolder();

            holder.img_icon = (ImageView) convertView.findViewById(R.id.messgather_s_peopleicon);
            holder.txt_content1 = (TextView) convertView.findViewById(R.id.messgather_s_name);
            holder.txt_content2 = (TextView) convertView.findViewById(R.id.messgatherphone);
            holder.txt_content3 = (TextView) convertView.findViewById(R.id.messgather_s_address);
            holder.constraintLayout = (ConstraintLayout) convertView.findViewById(R.id.messgather_s_all);

            holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Activity activity=(Activity)mContext;
                    activity.finish();
                }
            });

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.img_icon.setImageResource(mData.get(position).getImgId());

        holder.txt_content1.setText(mData.get(position).getContent());

        holder.txt_content2.setText(mData.get(position).getContent1());

        holder.txt_content3.setText(mData.get(position).getContent2());

        return convertView;
    }

    //添加
    public void add(Data data){
        if(mData==null){
            mData=new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }
    //删除
    public void remove(int position){
        if(mData!=null){ mData.remove(position);}
        notifyDataSetChanged();
    }


    private class ViewHolder{
        ImageView img_icon;
        TextView txt_content1;
        TextView txt_content2;
        TextView txt_content3;
        ConstraintLayout constraintLayout;
    }

}